package cn.melon.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import cn.melon.mod.store.entity.GoodsInfo;

/**
 * @ClassName: JGoodsPanel
 * @author Hzy
 * 一个商品面板类（包含图片、名称、售价和购买人数）
 */
public class JGoodsPanel extends JPanel {
    private GoodsInfo goods;
    private String imgPath = "";

    public JGoodsPanel(GoodsInfo goods) {
        try {
            ClassLoader classLoader = getClass().getClassLoader();
            String pathTmp = classLoader.getResource("img/java从入门到放弃.png").getFile();
            imgPath = java.net.URLDecoder.decode(pathTmp, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.goods = goods;
        this.setPreferredSize(new Dimension(112, 157));
        this.setBackground(Color.WHITE);
        JLabel img = new JLabel(new ImageIcon(imgPath));
        img.setPreferredSize(new Dimension(110,106));
//        img.setMinimumSize(new Dimension(110,106));
//        img.setMaximumSize(new Dimension(110,106));
        img.setBackground(Color.RED);
        JLabel price = new JLabel("￥"+goods.getGoodsPrice());
        JLabel sale = new JLabel(goods.getGoodsSales()+"人付款");
        JLabel name = new JLabel(goods.getGoodsName());
        price.setFont(new Font("微软雅黑",Font.BOLD,12));
        price.setForeground(new Color (235,79,56));
        sale.setFont(new Font("微软雅黑",Font.BOLD,12));
        name.setFont(new Font("微软雅黑",Font.BOLD,13));
        
        this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        this.add(img);
        img.setAlignmentX(Component.CENTER_ALIGNMENT);
        this.add(Box.createGlue());
        Box priceAndSale = Box.createHorizontalBox();
        priceAndSale.add(price);
        priceAndSale.add(Box.createHorizontalGlue());
        priceAndSale.add(sale);
        this.add(priceAndSale);
        this.add(name);
//        name.setAlignmentY(BOTTOM_ALIGNMENT);
        name.setAlignmentX(CENTER_ALIGNMENT);
        this.setBorder(BorderFactory.createLineBorder(new Color(180,180,180)));
    }
}
